package cc.mcggdd.leah.boot.stock.controller;

import cc.mcggdd.leah.boot.stock.service.StockReceivableService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import cc.mcggdd.leah.boot.stock.model.form.StockReceivableForm;
import cc.mcggdd.leah.boot.stock.model.query.StockReceivableQuery;
import cc.mcggdd.leah.boot.stock.model.vo.StockReceivableVO;
import com.baomidou.mybatisplus.core.metadata.IPage;
import cc.mcggdd.leah.boot.common.result.PageResult;
import cc.mcggdd.leah.boot.common.result.Result;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import jakarta.validation.Valid;

/**
 * 应收前端控制层
 *
 * @author 
 * @since 2025-02-08 17:52
 */
@Tag(name = "应收接口")
@RestController
@RequestMapping("/api/v1/stockReceivables")
@RequiredArgsConstructor
public class StockReceivableController  {

    private final StockReceivableService stockReceivableService;

    @Operation(summary = "应收分页列表")
    @GetMapping("/page")
    @PreAuthorize("@ss.hasPerm('stock:stockReceivable:query')")
    public PageResult<StockReceivableVO> getStockReceivablePage(StockReceivableQuery queryParams ) {
        IPage<StockReceivableVO> result = stockReceivableService.getStockReceivablePage(queryParams);
        return PageResult.success(result);
    }

    @Operation(summary = "新增应收")
    @PostMapping
    @PreAuthorize("@ss.hasPerm('stock:stockReceivable:add')")
    public Result<Void> saveStockReceivable(@RequestBody @Valid StockReceivableForm formData ) {
        boolean result = stockReceivableService.saveStockReceivable(formData);
        return Result.judge(result);
    }

    @Operation(summary = "获取应收表单数据")
    @GetMapping("/{id}/form")
    @PreAuthorize("@ss.hasPerm('stock:stockReceivable:edit')")
    public Result<StockReceivableForm> getStockReceivableForm(
        @Parameter(description = "应收ID") @PathVariable Long id
    ) {
        StockReceivableForm formData = stockReceivableService.getStockReceivableFormData(id);
        return Result.success(formData);
    }

    @Operation(summary = "修改应收")
    @PutMapping(value = "/{id}")
    @PreAuthorize("@ss.hasPerm('stock:stockReceivable:edit')")
    public Result<Void> updateStockReceivable(
            @Parameter(description = "应收ID") @PathVariable Long id,
            @RequestBody @Validated StockReceivableForm formData
    ) {
        boolean result = stockReceivableService.updateStockReceivable(id, formData);
        return Result.judge(result);
    }

    @Operation(summary = "删除应收")
    @DeleteMapping("/{ids}")
    @PreAuthorize("@ss.hasPerm('stock:stockReceivable:delete')")
    public Result<Void> deleteStockReceivables(
        @Parameter(description = "应收ID，多个以英文逗号(,)分割") @PathVariable String ids
    ) {
        boolean result = stockReceivableService.deleteStockReceivables(ids);
        return Result.judge(result);
    }
}
